//
//  HRZCreateStoreViewController.m
//  EMarket
//
//  Created by 石头 on 2018/9/6.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZCreateStoreViewController.h"
// Controllers

// Models
#import "HRZAttestationModel.h"
#import "HRZUserInfo.h"
// Views

// Vendors

// Categories

// Others
#import "HRZTool.h"
#import "HRZGlobalNetManager.h"
#import "TZImageManager.h"
#import "TZImagePickerController.h"


@interface HRZCreateStoreViewController ()<TZImagePickerControllerDelegate>
{
    HRZAttestationModel *_model;
}
@property (weak, nonatomic) IBOutlet UITextField *mStoreName;
@property (weak, nonatomic) IBOutlet UITextField *mContex;
@property (weak, nonatomic) IBOutlet UITextField *mPhone;
@property (weak, nonatomic) IBOutlet UIImageView *mImageStroe;
@property (weak, nonatomic) IBOutlet UIImageView *mImageFoot;

@property (weak, nonatomic) IBOutlet UIView *mWatingView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *mWaitingAct;
@property (weak, nonatomic) IBOutlet UIView *mWatingViewFood;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *mWatingActFood;

@end

@implementation HRZCreateStoreViewController

#pragma mark - ----------  lazyLoad  ----------

#pragma mark - ----------  lifeCycle  ----------
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpUI];
    
    _model = [HRZAttestationModel new];
}

#pragma mark - ----------  UI  ----------
- (void)setUpUI {
    
    self.title = @"门店注册";
}

#pragma mark - ----------  Action  ----------

- (void)createShop {
    
    _model.fname = _mStoreName.text;
    _model.sname = [_mStoreName.text substringToIndex:1];
    _model.contacts = _mContex.text;
    _model.phone = _mPhone.text;
    _model.shopId = [HRZTool getUserShopInfo].userId;
    
    _model.busiLicense = @"http://speedmaterial.oss-cn-qingdao.aliyuncs.com/imges/head_portrait/1536290452046.png";
    _model.foodPermit = @"http://speedmaterial.oss-cn-qingdao.aliyuncs.com/imges/head_portrait/1536290452046.png";
    
    [[HRZGlobalNetManager getGlobalNetManager] onUserShopAttestationWith:_model complete:^(NSString *storeId, BOOL isSuc) {
        if (isSuc) {
            HRZUserInfo *userInfo = [HRZTool getUserShopInfo];
            userInfo.storeId = storeId;
            [HRZTool setUserShopInfo:userInfo];
        }
    }];
}

- (void)uploadImage:(UIImage *)image result:(void (^)(NSString *res, BOOL isSuc))res{
    [[HRZGlobalNetManager getGlobalNetManager] onUserShopUploadImage:image complete:^(NSString *result, BOOL isSuc) {
        if (isSuc) {
            res(result, YES);
        }
        res(@"", NO);
    }];
}

#pragma mark - ----------  Delegate  ----------

#pragma mark - ----------  Other  ----------

- (IBAction)onShoreImageBtnClick:(id)sender {
    _mWatingView.hidden = NO;
    [_mWaitingAct startAnimating];
    [self pushTZImagePickerControllerComplete:^(UIImage *image) {
        _mImageStroe.image = image;
        [self uploadImage:image result:^(NSString *res, BOOL isSuc) {
            if (isSuc) _model.busiLicense = res;
            
            [_mWaitingAct stopAnimating];
            _mWatingView.hidden = YES;
        }];
    }];
}

- (IBAction)onFoodImageBtnClick:(id)sender {
    _mWatingViewFood.hidden = NO;
    [_mWatingActFood startAnimating];
    [self pushTZImagePickerControllerComplete:^(UIImage *image) {
        _mImageFoot.image = image;
        [self uploadImage:image result:^(NSString *res, BOOL isSuc) {
            if (isSuc) _model.foodPermit = res;
            _mWatingViewFood.hidden = YES;
            [_mWatingActFood stopAnimating];
        }];
    }];
}

- (IBAction)onAttrecStoreBtnClick:(id)sender {
    [self createShop];
}

#pragma mark - ----------  ImagePickerViewController  ----------
- (void)pushTZImagePickerControllerComplete:(void (^)(UIImage *image))image {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    
    imagePickerVc.allowPickingVideo = NO;
    
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        image(photos[0]);
    }];
     
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark - TZImagePickerControllerDelegate

@end
